﻿$(function() {

    //初始化布局
    if (typeof (InitDialog) == "function") {
        InitDialog();
    }

});

function InitDialog() {

    var mainDiv = "<div id=\"maindialog\" class=\"container backgroung\"><div class=\"container backgroung halfshow\"></div><div id=\"dialog\" class=\"dialog\"></div></div>";
    $(document.body).append(mainDiv);
    $("#maindialog").css("display", "none");

    //后台发布的Js方法
    var scriptObj = $("#SpanScript");
    if (scriptObj && scriptObj.length > 0) {
        var script = scriptObj.text();
        if (script.length > 0) {
            eval(script);
        }
    }
}

function ShowWindow(dialogWidth, dialogHeight) {

    $("#maindialog").css("display", "");

    var dialogObj = $("#dialog");
    if (dialogWidth > 0) {
        dialogObj.width(dialogWidth);
    }
    if (dialogHeight > 0) {
        dialogObj.height(dialogHeight);
    }

    var bodyObj = $("body");
    var bodyWidth = bodyObj.width();
    var bodyHeight = bodyObj.height();

    var dialogLeft = parseInt((bodyWidth - dialogWidth) / 2);
    var dialogTop = parseInt((bodyHeight - dialogHeight) / 2);
    dialogObj.css("left", dialogLeft);
    dialogObj.css("top", dialogTop);

}

function CloseDialog() {
    //$("#dialog").html("");
    $("#dialog iframe").attr("src", "");

    $("#maindialog").css("display", "none");
}

function Alert(msg, ok) {
    ShowWindow(300, 200);
    var title = "系统提示";
    CreateWindow("alert", title, msg, "", ok);
}

function Confirm(msg, ok, cancel) {
    ShowWindow(300, 200);
    var title = "系统提示";
    CreateWindow("confirm", title, msg, "", ok, cancel);
}

function Success(msg, ok) {
    ShowWindow(300, 200);
    var title = "系统提示";
    CreateWindow("success", title, msg, "", ok);
}

function Output(msg, ok) {

    var screenWidth = $(window).width();
    var dialogWidth = parseInt(screenWidth * 0.8);
    if (dialogWidth < 700) {
        dialogWidth = 700;
    }
    dialogWidth = Math.floor(dialogWidth / 100) * 100;

    var screenHeight = $(window).height();
    var dialogHeight = parseInt(screenHeight * 0.8);
    if (dialogHeight < 450) {
        dialogHeight = 450;
    }
    dialogHeight = Math.floor(dialogHeight / 50) * 50;

    ShowWindow(dialogWidth, dialogHeight);
    var title = "信息输出";
    CreateWindow("output", title, msg, "", ok);
}

function CommonDialog(url, title, w, h, ret) {

    ShowWindow(w, h);

    var nullObj = undefined;
    CreateWindow("common", title, "", url, nullObj, nullObj, ret);

}

//针对外界方法
function ReturnDialogValue(v) {
    var parentObj = $(".ifameReturnValue", parent.document);
    parentObj.val(v);
    parentObj.click();
}

function CreateWindow(type, title, msg, url, ok, cancel, ret) {

    var dialog = $("#dialog");
    if (type == "common") {
        var a = "<div class=\"banner fullwidth\"><div class=\"bannerleft\"></div><div class=\"bannermid calcwidth\"><div class=\"title calcwidth\">" + title + "</div><div class=\"closebutton\"></div></div><div class=\"bannerright\"></div></div><div class=\"clearboth\"></div><div class=\"content calcheight fullwidth\"><div class=\"bodyleft fullheight\"></div><div class=\"bodymid calcwidth fullheight\"><input type=\"text\" class=\"ifameReturnValue\" style=\"display:none;\" /><iframe class=\"container\" src=\"" + url + "\" frameborder=\"0\" scrolling=\"no\"></iframe></div><div class=\"bodyright fullheight\"></div></div><div class=\"clearboth\"></div><div class=\"foot fullwidth\"><div class=\"footleft\"></div><div class=\"footmid calcwidth\"></div><div class=\"footright\"></div></div>";
        dialog.html(a);
    }
    else if (type == "output") {
        var a = "<div class=\"banner fullwidth\"><div class=\"bannerleft\"></div><div class=\"bannermid calcwidth\"><div class=\"title calcwidth\">" + title + "</div><div class=\"closebutton\"></div></div><div class=\"bannerright\"></div></div><div class=\"clearboth\"></div><div class=\"content calcheight fullwidth\"><div class=\"bodyleft fullheight\"></div><div class=\"bodymid calcwidth fullheight\"><input type=\"text\" class=\"ifameReturnValue\" style=\"display:none;\" /><textarea class=\"container\">" + msg + "</textarea></div><div class=\"bodyright fullheight\"></div></div><div class=\"clearboth\"></div><div class=\"foot fullwidth\"><div class=\"footleft\"></div><div class=\"footmid calcwidth\"></div><div class=\"footright\"></div></div>";
        dialog.html(a);
    }
    else {
        var a = "<div class=\"banner fullwidth\"><div class=\"bannerleft\"></div><div class=\"bannermid calcwidth\"><div class=\"title calcwidth\">" + title + "</div><div class=\"closebutton\"></div></div><div class=\"bannerright\"></div></div><div class=\"clearboth\"></div><div class=\"content calcheight fullwidth\"><div class=\"bodyleft fullheight\"></div><div class=\"bodymid calcwidth fullheight " + type + "\"><div class=\"img\"></div><div class=\"text\">" + msg + "</div><div class=\"clearboth\"></div><div class=\"buttons\"><input type=\"button\" value=\"确定\" class=\"okbutton\" /><input type=\"button\" value=\"取消\" class=\"cancelbutton\" /></div></div><div class=\"bodyright fullheight\"></div></div><div class=\"clearboth\"></div><div class=\"foot fullwidth\"><div class=\"footleft\"></div><div class=\"footmid calcwidth\"></div><div class=\"footright\"></div></div>";
        dialog.html(a);
    }

    //重新排列布局
    CalcLayout(dialog);


    //赋予事件
    $(".closebutton").click(function() {
        CloseDialog();
        if (cancel) {
            cancel();
        }
    });

    $(".okbutton").click(function() {
        CloseDialog();
        if (ok) {
            ok();
        }
    });

    $(".cancelbutton").click(function() {
        CloseDialog();
        if (cancel) {
            cancel();
        }
    });

    $(".ifameReturnValue").click(function() {

        CloseDialog();

        if (ret) {
            var v = this.value;
            this.value = "";
            ret(v);
        }
    });
}
